package com.JackTang.fruit.dao.impl;

import com.JackTang.fruit.dao.FruitDAO;
import com.JackTang.fruit.pojo.Fruit;
import com.myssm.basedao.BaseDAO;

import java.util.List;

/**
 * @author Jack Tang
 * @description
 * @create 2023-05-04 19:51
 */
public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO{
    @Override
    public List<Fruit> getFruitList(Integer pageNo){
        return super.executeQuery("select * from t_fruit limit ? , 5", (pageNo-1)*5);
    }

    @Override
    public Fruit getFruitByFid(Integer fid) {
        return super.load("select * from t_fruit where fid = ?" , fid);
    }

    @Override
    public void updateFruit(Fruit fruit){
        String sql = "update t_fruit set fname = ? , price = ? , fcount = ? , remark = ? where fid = ?";
        super.executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark(),fruit.getFid());
    }

    @Override
    public void deleteFruit(Integer fid) {
        String sql = "delete from t_fruit where fid = ?";
        super.executeUpdate(sql,fid);
    }

    @Override
    public void addFruit(Fruit fruit) {
        String sql = "insert into t_fruit(0,?,?,?,?)";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());
    }

    @Override
    public int getFruitCount() {
        //报错  ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
        return ((Long)super.executeComplexQuery("select count(*) from t_fruit")[0]).intValue();
    }
}